<template>
	<view class="scroll-view-container">
		 <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
			 <block v-for="(item,i) in cateList" :key="i">
				 <view class="left-scroll-view-item" :id="i==active?'active':''" @click="activeChange(i)">{{item.cat_name}}</view>
			 </block>
		  </scroll-view>
		  <!-- 右侧的滚动视图区域 -->
		  <scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scroll">
			<view v-for="(item,i) in cateLeve2" :key="i">
				<!-- 菜单的二级分类 -->
				<view class="cate-lv2-title">{{item.cat_name}}</view>
				<!-- 菜单的三级分类 -->
				<view class="cate-lv3-list">
					<view class="cate-lv3-item" v-for="(list,a) in item.children" :key="a" @click="gotoGoodsList(list.cat_id)">
						<image class="cate-lv3-image" :src="list.cat_icon"></image>
						<text class="cate-lv3-title">{{list.cat_name}}</text>
					</view>
				</view>
			</view>
		  </scroll-view>
	</view>
</template>

<script>
	import tabbar from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [tabbar],
		data() {
			return {
				wh: 0,
				//一级菜单
				cateList: [],
				//二级菜单
				cateLeve2: [],
				//三级分类
				cateLeve3: [],
				active: 0,
				//设置滚动条距离
				scroll: 0
			};
		},
		onLoad() {
			// 获取当前系统的信息
			const sysInfo = uni.getSystemInfoSync()
			// // 为 wh 窗口可用高度动态赋值
			this.wh = sysInfo.windowHeight
			//获取分类数据
			this.getCateList()
			
		},
		methods: {
			//获取菜单信息
			async getCateList(){
				const {data:res} = await uni.$http.get("/api/public/v1/categories")
				this.cateList = res.message
				//获取二级菜单信息
				this.cateLeve2 = res.message[0].children
				//获取三级菜单的信息
				const lev3 = res.message[0].children[0].children;
				if(lev3 == null){
					return;
				}
				this.cateLeve3 = lev3
			},
			activeChange(id){
				this.active = id
				this.cateLeve2 = this.cateList[id].children
				this.scroll = this.scroll == 0 ? 1 : 0
			},
			gotoGoodsList(id){
				uni.navigateTo({
					url:"/subpkg/goods_list/goods_list?cid="+id
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll-view-container{
	display: flex;
	.left-scroll-view{
		float: left;
		width: 240rpx;
		background-color: antiquewhite;
		.left-scroll-view-item{
			text-align: center;
			font-size: 12px;
			line-height: 60px;
		}
		#active{
			background-color: white;
			position: relative;
			&::before{
				content: ' ';
				width: 3px;
				height: 30px;
				background-color: red;
				display: block;
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
			}
		}
		
	}
	.right-scroll-view{
		background-color: white;
		display: flex;
		.cate-lv2-title{
			text-align: center;
			font-size: 12px;
			font-weight: bold;
			padding: 15px 0;
		}
		//三级分类
		.cate-lv3-list{
			display: flex;
			flex-wrap: wrap;
			.cate-lv3-item{
				width: 33.3%;
				flex-wrap: wrap;
				display: flex;
				align-items: center;
				margin-top: 20px;
				flex-direction: column;
				.cate-lv3-image{
					width: 60px;
					height: 60px;
				}
				.cate-lv3-title{
					font-size: 12px;
				}
			}
		}
	}
}
</style>
